package com.chance.cc.crawler.development.command.job.domain.douyin.custom;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.script.crawler.DomainScriptKey;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.development.scripts.douyin.phone.DYPhoneSearchForCustomMI5SADBScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerPhoneJob;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author bx
 * @date 2020/12/6 0006 12:44
 */
public class DouyinSearchCustomPhoneCrawlerScheduleJob {

    private static String domainId = "douyin";
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);
    public static final String crawlerKey = "douyin_search_custom_1-phone-realtime";

    public static void main(String[] args) {
        publishDYPhoneCrawlerSheduleJobInfo();
    }

    public static void publishDYPhoneCrawlerSheduleJobInfo(){
        CrawlerPhoneJob crawlerPhoneJob = dyPhoneSchduler(domainId);

        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerPhoneJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.phone.enumVal());
        crawlerScheduleJob.setNote("抖音关键词搜索自定义定时调度");
        crawlerScheduleJob.setCrawlerKey(crawlerKey);
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布phone作业：" + page.getRawText());

    }

    public static CrawlerPhoneJob dyPhoneSchduler(String domainId) {

        CrawlerPhoneJob crawlerPhoneJob = new CrawlerPhoneJob();
        crawlerPhoneJob.setDomain(domainId);
        crawlerPhoneJob.setSite(StringUtils.joinWith("-","search","custom","1"));
        crawlerPhoneJob.setDevicesType(StringUtils.joinWith("-",domainId,"search","custom"));
        crawlerPhoneJob.setLoaderDomainKeys(true);
        Map<String, String> dataMap = Maps.newHashMap();
        dataMap.put("phone_swipe_numbers","20");
        crawlerPhoneJob.setJobDataMap(dataMap);
        String phoneScriptHandlerKey = StringUtils.joinWith("/",
                DomainScriptKey.scriptDomainPrefix(domainId, CrawlerEnum.CrawlerScriptType.scheduler),
//                "com.chance.cc.crawler.sheduler.job.handler.phone.DYPhoneSearchMI5SADBScript");
                DYPhoneSearchForCustomMI5SADBScript.class.getCanonicalName());
        crawlerPhoneJob.addPhoneScriptHandler(phoneScriptHandlerKey);
        return crawlerPhoneJob;
    }
}
